﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using ZaidimasSandelys.CustomDrawTools;
using ZaidimasSandelys.Models.DataStructures;
using ZaidimasSandelys.Models.Interfaces;
using Ninject;

namespace ZaidimasSandelys.Models.Classes
{
    /// <summary>
    /// Pagrindinio lango modelis (menu ir drop down elementai)
    /// </summary>
    public class MainWindowModel : IMainWindowModel, INotifyPropertyChanged
    {
        /// <summary>
        /// MP3 failo vardas nuskaitytas iš <code>App.config</code>
        /// </summary>
        public string Mp3FileName { get; set; }

        /// <summary>
        /// Pagrindinis meniu užsikraus pats, kai tik kas nos kreipsis į <code>MenuItems</code>
        /// </summary>
        private IEnumerable<MainMenuItem> _menuItems = null;
        public IEnumerable<MainMenuItem> MenuItems 
        {
            get
            {
                if (_menuItems == null)
                    _menuItems = _appData.GetMainMenu();
                return _menuItems;
            }
        }
        public MainMenuItem? SelectedComplexity { get; set; }
        /// <summary>
        /// Žaidimo lygis <see cref="GameLevel"/>
        /// </summary>
        public GameLevel GameComplexity
        {
            get
            {
                return SelectedComplexity.HasValue ? SelectedComplexity.Value.Value : GameLevel.Unassigned;
            }
            set
            {
                SelectedComplexity = MenuItems.Where(x => x.Value == value).FirstOrDefault();
                NotifyPropertyChanged("SelectedComplexity");
            }
        }

        /// <summary>
        /// Žmogeliukas
        /// </summary>
        public PersonVisual Person { get; set; }

        private ObservableCollection<VariantMenuItem> _gameVariants = null;
        public ObservableCollection<VariantMenuItem> GameVariants
        {
            get
            {
                if (_gameVariants == null)
                    _gameVariants = new ObservableCollection<VariantMenuItem>(_appData.GetGameVariants(GameComplexity));
                return _gameVariants;
            }
        }
        public VariantMenuItem? SelectedVariant { get; set; }
        /// <summary>
        /// Pasirinktas variantas
        /// </summary>
        public short GameVariant
        {
            get
            {
                return SelectedVariant.HasValue ? SelectedVariant.Value.Value : (short)-1;
            }
            set
            {
                SelectedVariant = GameVariants.Where(x => x.Value == value).FirstOrDefault();
                NotifyPropertyChanged("SelectedVariant");
            }
        }

        /// <summary>
        /// "Jūs laimėjot" dialogo konstantos
        /// </summary>
        private int _MovementCount; 
        public int MovementCount 
        {
            get { return _MovementCount; }
            set
            {
                if (_MovementCount != value)
                {
                    _MovementCount = value;
                    NotifyPropertyChanged("MovementCount");
                }
            }
        }

        /// <summary>
        /// <code>true></code> - žaidimas pradėtas
        /// </summary>
        private bool _IsStarted;
        public bool IsStarted 
        {
            get { return _IsStarted; }
            set
            {
                _IsStarted = value;
                NotifyPropertyChanged("StartButtonText");
            }
        }

        private string[] _startButtonTexts;
        public string StartButtonText
        {
            get { return _startButtonTexts[_IsStarted ? 1 : 0]; }
        }

        public bool StartButtonIsEnabled
        {
            get { return this.GameComplexity != GameLevel.Unassigned; }
        }

        public Visibility VariantComboBoxIsVisible
        {
            get 
            {
                return _gameVariants == null || _gameVariants.Count < 1 ? Visibility.Hidden : Visibility.Visible;
            }
        }

        public string GameVariantText { get; set; }

        public void LoadGameVariants(ComboBox cmbBox)
        {
            GameVariants.Clear();
            foreach (VariantMenuItem item in _appData.GetGameVariants(GameComplexity))
                GameVariants.Add(item);
            if (GameVariants.Count > 0)
            {
                cmbBox.SelectedIndex = 0;
            }
            else
            {
                cmbBox.SelectedIndex = -1;
            }
            NotifyPropertyChanged("VariantComboBoxIsVisible");
        }

        private void LoadMultilanguageConstants()
        {
            string[] mltTexts = _appData.GetMultilanguageConstants("Main");
            if (mltTexts != null && mltTexts.Length > 0)
                GameVariantText = mltTexts[0];
            string[] WonMessages = _appData.GetMultilanguageConstants("YouWon");
            if (WonMessages == null || WonMessages.Length < 1)
                _MovementsLabelText = _StatusLabelText = String.Empty;
            else
            {
                _StatusLabelText = WonMessages[0];
                _MovementsLabelText = WonMessages.Length < 2 ? String.Empty : WonMessages[1];
            }
        }

        private string _MovementsLabelText;
        public string MovementsLabelText
        {
            get { return _MovementsLabelText; }
            set
            {
                _MovementsLabelText = value;
                NotifyPropertyChanged("MovementsLabelText");
            }
        }
        private string _StatusLabelText;
        public string StatusLabelText
        {
            get { return _StatusLabelText; }
            set
            {
                _StatusLabelText = value;
                NotifyPropertyChanged("StatusLabelText");
            }
        }

        private IAppData _appData;
#if DEBUG
        public IAppData AppData 
        {
            get { return this._appData; }
        }
#endif

        [Inject]
        public MainWindowModel(IAppData appData, string mp3FileName)
        {
            this._appData = appData;
            this.Mp3FileName = mp3FileName;

            this._menuItems = _appData.GetMainMenu();
            this._startButtonTexts = _appData.GetStartButtonText();
            this._IsStarted = false;
            this.Person = null;
            this.LoadMultilanguageConstants();

            GameComplexity = GameLevel.Unassigned;
        }

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) 
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion

    }
}
